<?php


namespace app\validate\api;


use app\exception\BaseException;
use app\exception\ParameterException;
use think\Request;
use think\Exception;

use think\Validate;

class BaseValidate extends Validate
{
    /**
     * @return bool
     * @throws Exception
     */
   public function goCheck(): bool
   {
       //获取http传入的参数
       //对所有参数做校验
       $params  = \think\facade\Request::param();
       $result = $this->batch()->check($params);
       if(!$result){
           throw new ParameterException([
               'msg'=> implode(' 且 ',$this->error)
           ]);
       }else{
           return true;
       }
   }

    protected function isPositiveInteger($value,$rule='',$data='',$field='')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return $field.'必须是正整数';
        }
    }
    
    protected function isNotEmpty($value,$rule='',$data='',$field=''): bool
    {
       if(empty($value)){
           return false;
       }else{
           return true;
       }
    }

    /**
     * @param $arrays
     *
     * @return array
     * @throws ParameterException
     */
    public function getDataByRule($arrays): array
    {
       if(array_key_exists('user_id',$arrays)|array_key_exists('uid',$arrays)){
           //不允许包含user_id 或者 uid 防止恶意覆盖user_id
           throw new ParameterException([
               'msg'=>'参数中包含非法的参数user_id 或者 uid'
           ]);
       }
       $newArray = [];
       foreach ($this->rule as $key => $value){
           $newArray[$key] = $arrays[$key];
       }
       return $newArray;
    }

    public function isMobile($value){
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule,$value);
        if($result){
            return true;
        }else{
            return false;
        }
    }
}